package com.meng.utils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


/**
 * 工具类
 *
 * @author MENG
 * @version 2017/12/20
 * @see
 */
public class Utils
{
    /**
     *
     * MD5 加密
     *
     * @param str 加密前的字符串
     * @return newStr 加密后的字符串
     * @throws NoSuchAlgorithmException
     * @throws RuntimeException
     */
    public static String encodeByMd5(String str) throws NoSuchAlgorithmException
    {
        String newStr = "";
        try
        {
            MessageDigest md = null;

            md = MessageDigest.getInstance("MD5");

            md.update(str.getBytes());

            newStr = new BigInteger(1, md.digest()).toString(16);
        }
        catch (NoSuchAlgorithmException e)
        {
            throw new NoSuchAlgorithmException("MD5密码加密出错", e);
        }

        return newStr;
    }

    /**
     * Is empty or is null
     *
     * @param str
     * @return boolean
     */
    public boolean isEmptyOrNull(String str)
    {
        if (str == null || "".equals(str))
        {
            return true;
        }

        return false;
    }
}

